package vip.websky.project.user.service.impl;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.plugins.Page;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Repository;
import org.springframework.stereotype.Service;
import vip.websky.project.common.error.CommonsRuntimeException;
import vip.websky.project.common.error.ErrorCode;
import vip.websky.project.common.error.ErrorMessage;
import vip.websky.project.common.utils.DateUtils;
import vip.websky.project.common.utils.ObjectConvertUtils;
import vip.websky.project.core.log.KingLog;
import vip.websky.project.module.bo.user.SysUserBO;
import vip.websky.project.module.dto.RequestDTO;
import vip.websky.project.module.dto.ResponseDTO;
import vip.websky.project.module.dto.request.user.AddSysUserReqDTO;
import vip.websky.project.module.dto.request.user.EditSysUserReqDTO;
import vip.websky.project.module.dto.request.user.GetSysUserReqDTO;
import vip.websky.project.module.dto.response.user.SysUserRespDTO;
import vip.websky.project.module.entity.user.SysUser;
import vip.websky.project.user.config.UserConst;
import vip.websky.project.user.dao.UserDao;
import vip.websky.project.user.error.UserErrorCode;
import vip.websky.project.user.error.UserErrorMessage;
import vip.websky.project.user.server.UserServer;
import vip.websky.project.user.service.UserService;

import javax.annotation.Resource;
import java.util.List;

/**
 * 系统用户 接口实现类
 *
 * @Author Yong.Yang E-mail:yang2048@qq.com
 * @Create 2017/8/13-19:58
 */
@Slf4j
@Service
public class UserServiceImpl implements UserService {

    @Autowired private UserServer userServer;

    /**
     * 1. 创建用户
     *
     * @param requestDTO
     */
    @Override
    @KingLog
    public ResponseDTO addUser(RequestDTO requestDTO) {
        ResponseDTO<SysUserRespDTO> responseDTO = new ResponseDTO<>();
        try {
            //0.请求参数
            AddSysUserReqDTO addSysUser = JSON.parseObject(requestDTO.getParam(), AddSysUserReqDTO.class);
            // TODO: 校验参数
            //1.参数转换
            SysUserBO sysUserBO = ObjectConvertUtils.copyToDest(addSysUser, SysUserBO.class);
            //2.处理
            sysUserBO.setPassword(UserConst.PASSWORD_DEFAULT);
            sysUserBO.setAvatar(UserConst.AVATAR_DEFAULT);
            sysUserBO.setCreateTime(DateUtils.getCurrentTimeFormat(DateUtils.DATETIME_SIMPLE_MS));
            sysUserBO = userServer.insertUser(sysUserBO);
            //3.返回结果
            if (sysUserBO != null) {
                SysUserRespDTO respDTO = new SysUserRespDTO();
                ObjectConvertUtils.convert(sysUserBO, respDTO);
                responseDTO.setResult(respDTO);
            }else {
                responseDTO.setStatusCode(UserErrorCode.USER_ADD_FAIL);
                responseDTO.setStatusMessage(UserErrorMessage.USER_ADD_FAIL);
                log.info("添加用户失败");
            }
        } catch (CommonsRuntimeException e) {
            responseDTO.setStatusCode(e.getCode());
            responseDTO.setStatusMessage(e.getMessage());
            log.error("自定义异常:{},{}", e.getCode(), e.getMessage());
        } catch (Exception e) {
            responseDTO.setStatusCode(ErrorCode.UNEXPECTED_ERROR);
            responseDTO.setStatusMessage(ErrorMessage.UNEXPECTED_ERROR);
            log.error("系统异常:{}", e);
        }
        return responseDTO;
    }

    /**
     * 2. 查询用户
     *
     * @param requestDTO
     * @return
     */
    @Override
    @KingLog
    public ResponseDTO getUser(RequestDTO requestDTO) {
        ResponseDTO<SysUserRespDTO> responseDTO = new ResponseDTO<>();
        try {
            //0.请求参数
            GetSysUserReqDTO getSysUser = JSON.parseObject(requestDTO.getParam(), GetSysUserReqDTO.class);
            // TODO: 校验参数
            //1.参数转换
            SysUserBO sysUserBO = ObjectConvertUtils.copyToDest(getSysUser, SysUserBO.class);
            //2.处理
            sysUserBO = userServer.selectUser(sysUserBO);
            //3.返回结果
            if (sysUserBO != null) {
                SysUserRespDTO respDTO = new SysUserRespDTO();
                ObjectConvertUtils.convert(sysUserBO, respDTO);
                responseDTO.setResult(respDTO);
            }else {
                responseDTO.setStatusCode(UserErrorCode.USER_NOT_EXIST);
                responseDTO.setStatusMessage(UserErrorMessage.USER_NOT_EXIST);
                log.info("获取用户信息失败");
            }
        } catch (CommonsRuntimeException e) {
            responseDTO.setStatusCode(e.getCode());
            responseDTO.setStatusMessage(e.getMessage());
            log.error("自定义异常:{},{}", e.getCode(), e.getMessage());
        } catch (Exception e) {
            responseDTO.setStatusCode(ErrorCode.UNEXPECTED_ERROR);
            responseDTO.setStatusMessage(ErrorMessage.UNEXPECTED_ERROR);
            log.error("系统异常:{}", e);
        }
        return responseDTO;
    }

    /**
     * 3. 查询用户列表
     *
     * @param requestDTO
     */
    @Override
    @KingLog
    public ResponseDTO getUserList(RequestDTO requestDTO) {
        ResponseDTO responseDTO = new ResponseDTO<>();
        try {
            //请求参数
            GetSysUserReqDTO getSysUser = JSON.parseObject(requestDTO.getParam(), GetSysUserReqDTO.class);
            //TODO 参数校验

            //参数转换
            SysUserBO sysUserBO = ObjectConvertUtils.copyToDest(getSysUser, SysUserBO.class);
            //处理
            Page<SysUserBO> sysUserBOPage = userServer.selectUserList(sysUserBO);
            //返回
            if (sysUserBOPage.getRecords() != null) {
                //整理转化数据对象
                List<SysUserRespDTO> sysUserRespDTOS = ObjectConvertUtils.copyToList(sysUserBOPage.getRecords(), SysUserRespDTO.class);
                responseDTO.setResult(sysUserRespDTOS);//数据
                responseDTO.setTotalCount(String.valueOf(sysUserBOPage.getTotal()));//总条数
                responseDTO.setTotalPages(String.valueOf(sysUserBOPage.getPages()));//总页数
            } else {
                responseDTO.setStatusCode(UserErrorCode.USER_NOT_EXIST);
                responseDTO.setStatusMessage(UserErrorMessage.USER_NOT_EXIST);
                log.info("获取用户列表失败");
            }
        } catch (CommonsRuntimeException e) {
            responseDTO.setStatusCode(e.getCode());
            responseDTO.setStatusMessage(e.getMessage());
            log.error("自定义异常:{},{}", e.getCode(), e.getMessage());
        } catch (Exception e) {
            responseDTO.setStatusCode(ErrorCode.UNEXPECTED_ERROR);
            responseDTO.setStatusMessage(ErrorMessage.UNEXPECTED_ERROR);
            log.error("系统异常:{}", e);
        }
        return responseDTO;
    }

    /**
     * 4. 修改用户信息
     * @param requestDTO
     * @return
     */
    @Override
    @KingLog
    public ResponseDTO editUser(RequestDTO requestDTO) {
        ResponseDTO<SysUserRespDTO> responseDTO = new ResponseDTO<>();
        try {
            //0.请求参数
            EditSysUserReqDTO editSysUser = JSON.parseObject(requestDTO.getParam(), EditSysUserReqDTO.class);
            // TODO: 校验参数
            //1.参数转换
            SysUserBO sysUserBO = ObjectConvertUtils.copyToDest(editSysUser, SysUserBO.class);
            //2.处理0
            sysUserBO = userServer.updateUser(sysUserBO);
            //3.返回结果
            if (sysUserBO != null) {
                SysUserRespDTO respDTO = new SysUserRespDTO();
                ObjectConvertUtils.convert(sysUserBO, respDTO);
                responseDTO.setResult(respDTO);
            }else {
                responseDTO.setStatusCode(UserErrorCode.USER_EDIT_FAIL);
                responseDTO.setStatusMessage(UserErrorMessage.USER_EDIT_FAIL);
                log.info("修改用户信息失败");
            }
        } catch (CommonsRuntimeException e) {
            responseDTO.setStatusCode(e.getCode());
            responseDTO.setStatusMessage(e.getMessage());
            log.error("自定义异常:{},{}", e.getCode(), e.getMessage());
        } catch (Exception e) {
            responseDTO.setStatusCode(ErrorCode.UNEXPECTED_ERROR);
            responseDTO.setStatusMessage(ErrorMessage.UNEXPECTED_ERROR);
            log.error("系统异常:{}", e);
        }
        return responseDTO;
    }

    /**
     * 5. 删除用户信息
     * @param requestDTO
     * @return
     */
    @Override
    @KingLog
    public ResponseDTO removeUser(RequestDTO requestDTO) {
        ResponseDTO<Boolean> responseDTO = new ResponseDTO<>();
        try {
            //0.请求参数
            GetSysUserReqDTO getSysUser = JSON.parseObject(requestDTO.getParam(), GetSysUserReqDTO.class);
            // TODO: 校验参数
            //1.参数转换
            SysUserBO sysUserBO = ObjectConvertUtils.copyToDest(getSysUser, SysUserBO.class);
            //2.处理
            boolean result = userServer.delUser(sysUserBO);
            //3.返回结果
            if (result) {
                responseDTO.setResult(result);
            }else {
                responseDTO.setStatusCode(UserErrorCode.USER_NOT_EXIST);
                responseDTO.setStatusMessage(UserErrorMessage.USER_NOT_EXIST);
                log.info("删除用户信息失败");
            }
        } catch (CommonsRuntimeException e) {
            responseDTO.setStatusCode(e.getCode());
            responseDTO.setStatusMessage(e.getMessage());
            log.error("自定义异常:{},{}", e.getCode(), e.getMessage());
        } catch (Exception e) {
            responseDTO.setStatusCode(ErrorCode.UNEXPECTED_ERROR);
            responseDTO.setStatusMessage(ErrorMessage.UNEXPECTED_ERROR);
            log.error("系统异常:{}", e);
        }
        return responseDTO;
    }

    @Override
    @KingLog
    public ResponseDTO findUserBy(RequestDTO requestDTO) {
        return null;
    }

}
